﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.DirectoryServices;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using  BusinessEntity;
using BusinessLogic;
using CentalineWeb.Common;

namespace CentalineWeb
{
    public partial class CommitFixReport : System.Web.UI.Page
    {
        ArticleManager am = new ArticleManager();
        RepairManager rm = new RepairManager();
        public string DeptListJson = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindArticle(); //调用绑定方法
            }
            DeptListJson = new DeptManager().SanJiShiChangDeptListToLevelJson(new DeptManager().GetAllValid() );
            var user = Session["user"] as User;
            if (user != null)
                txtStaff.Text = user.FullName;
            (this.Master.FindControl("lblTitle") as Label).Text = "物品报修";

        }
        //绑定报修种类
       private void BindArticle()
       {
           this.ddlArticle.DataSource = am.ListAll();
           this.ddlArticle.DataTextField = "troubleDesc";
           this.ddlArticle.DataValueField = "articleId";
           this.ddlArticle.DataBind();
           this.ddlArticle.Items.Insert(0, new ListItem("==请选择==","-1"));
       }
       #region 保存
       //将信息保存
        private void SubmitMessage()
        {
            var orderId = "BX" + DateTime.Now.ToString("yyyyMMddHHmmssff");
            var model = new Repairs()
            {
                WordOrderId = orderId,
                CompId =  txtNumber.Text.Trim(),
                ApplyAddres = txtAddress.Text.Trim(),
                ApplyEmpName = new UserManager().GetIdByUserId((Session["user"] as User).UserId),
                ApplyTime = DateTime.Now,
                Article = new Article() { ArticleId = Convert.ToInt32(ddlArticle.SelectedValue) },
                Email = txtMail.Text.Trim(),
                Phone = txtPhone.Text.Trim(),
                TroubleDesc = txtDesc.Text
            };
            var result = rm.AddOne(model);
            if (result > 0)
            {
                var sheetHistoryModel = new SheetHistory()
                {
                    OpDate=DateTime.Now,
                    OperId = new UserManager().GetIdByUserId((Session["user"] as User).UserId), //外键
                    OpId = (int)Operation.ApplyFix,
                    SheetId=new RepairManager().GetIdByWorkId(orderId),
                    Remark=""
                };
                new SheetHistoryManager().AddOne(sheetHistoryModel);
                this.ClientScript.RegisterStartupScript(typeof(string),"success","alert('提交成功！您的工单将在十分钟后自动受理，受理后不可更改。')",true);
                btnRewrite_Click(null, null);

                var timer = new WorkTimer();
                timer.NewTimer(orderId, Convert.ToInt32(ConfigurationManager.AppSettings["delayAsignTime"]));
            }
        }

        //提交保修单
       protected void btnSubmit_Click(object sender, EventArgs e)
       {
           if (Page.IsValid)
           {
              
              SubmitMessage();
              Response.Redirect("MySheet.aspx");
           }

       }
       #endregion

       #region 清空
        
        //清空文本，重写
       protected void btnRewrite_Click(object sender, EventArgs e)
       {
           this.ddlArticle.SelectedIndex = 0;
           this.txtNumber.Text = "";
           this.txtDesc.Text = "";
           this.txtMail.Text = "";
           this.txtPhone.Text = "";
       }
       #endregion
    }
}